package com.spring.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @version 1.0.0
 * @Date: 2022/6/16 20:42
 * @Author ZhuYouBin
 * @Description
 */
@Controller
public class RespController {

    @RequestMapping("resp01")
    public String resp01(HttpServletRequest request) {
        // 设置响应数据,保存到request域对象
        request.setAttribute("respData", "通过HttpServletRequest获取响应数据");
        return "resp";
    }

    @RequestMapping("resp02")
    public String resp02(HttpSession session) {
        // 设置响应数据,保存到session域对象
        session.setAttribute("respData", "通过HttpSession获取响应数据");
        return "resp";
    }

    @RequestMapping("resp03")
    public String resp03(Model model) {
        // 设置响应数据,保存到model对象
        model.addAttribute("model", "通过Model获取响应数据");
        return "resp";
    }

    @RequestMapping("resp04")
    public ModelAndView resp04() {
        // 创建 ModelAndView 对象
        ModelAndView mav = new ModelAndView();
        // 设置响应数据,保存到ModelAndView对象
        mav.addObject("modelAndView", "通过ModelAndView返回响应数据");
        // 设置视图名称: 需要跳转到哪个页面
        mav.setViewName("resp");
        return mav;
    }

    @RequestMapping("resp05")
    public String resp05(ModelMap modelMap) {
        // 设置响应数据, 保存到ModelMap对象
        modelMap.addAttribute("modelMap", "通过ModelMap返回响应数据");
        return "resp";
    }

    @RequestMapping("resp06")
    public String resp06(Map<String, Object> map) {
        // 设置响应数据, 保存到Map对象
        map.put("map", "通过Map返回响应数据");
        return "resp";
    }

}
